﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_News_ctrNewsList : System.Web.UI.UserControl
{
    private int nCurrentLang = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    private int nPageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        if (!IsPostBack)
        {
            this.setTitle();
            this.BindData(1);
        }
    }
    private void BindData(int CurrentPageIndex)
    {
        try
        {
            News objNews = new News();
            int nDefaultLang = Support.GetDefaultLanguage();
            int nCurrentLang = Support.GetCurrentLanguage();
            int nPageCount = 0;
            DataTable dtbNews = objNews.Search(0, 0, "", 0, nDefaultLang, nCurrentLang, 1, 1, nPageSize, ref nPageCount);
            rptNews.DataSource = dtbNews;
            rptNews.DataBind();

            Pager.ItemCount = nPageCount;
            VisiblePaging(nPageCount);
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    protected void setTitle()
    {
        lblCategoryName.Text = MultipleLanguage.LBL_NEWS[nCurrentLang - 1];
    }
    protected void rptNews_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);

        switch (strCommand)
        {
            case "image":
                string sDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;
                Response.Redirect(sDetail);
                break;
        }
    }
    protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnNewsImage = (ImageButton)e.Item.FindControl("btnNewsImage");
            HyperLink lnkNewsTitle = (HyperLink)e.Item.FindControl("lnkNewsTitle");
           // HyperLink lnkReadMore = (HyperLink)e.Item.FindControl("lnkReadMore");
            Literal ltrNews = (Literal)e.Item.FindControl("ltrNews");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));

           string sInitContent = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent"));
           if (sInitContent.Length > 100)
               sInitContent = ConvertData.TruncateString(sInitContent, 100) + "...";
           ltrNews.Text = sInitContent;
            btnNewsImage.ImageUrl = Config.ROOT + Config.IMAGE_NEWS_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
            btnNewsImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));

            lnkNewsTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;
            lnkNewsTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));

           // lnkReadMore.NavigateUrl = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;
          //  lnkReadMore.Text = MultipleLanguage.LINK_READMORE[nCurrentLang - 1];


        }
    }
    private void initDataPaging()
    {        
        Pager.PageSize = nPageSize;
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.BindData(currnetPageIndx);
    }
    private void VisiblePaging(int intPagecount)
    {
        if (intPagecount > nPageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }
}